package cn.iocoder.yudao.module.reading.service.community;

import cn.iocoder.yudao.module.reading.controller.app.community.vo.common.LikeToggleRespVO;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.work.WorkRespVO;
import cn.iocoder.yudao.module.reading.util.CommunityValidationUtils;
import cn.iocoder.yudao.module.reading.dal.dataobject.community.CommunityWorkCollectionDO;
import cn.iocoder.yudao.module.reading.dal.mysql.community.CommunityWorkCollectionMapper;
// import cn.iocoder.yudao.module.reading.service.book.BookService;
// import cn.iocoder.yudao.module.reading.service.bookset.BookSetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;

/**
 * 社区作品服务实现类
 *
 * @author 芋道源码
 */
@Service
@Slf4j
public class CommunityWorkServiceImpl implements CommunityWorkService {

    @Resource
    private CommunityWorkCollectionMapper workCollectionMapper;

    // @Resource
    // private BookService bookService;

    // @Resource
    // private BookSetService bookSetService;

    @Override
    public WorkRespVO getWorkDetail(Long workId, String workType, Long userId) {
        // 参数校验
        CommunityValidationUtils.validateId(workId, "作品ID");
        CommunityValidationUtils.validateWorkType(workType);

        WorkRespVO workDetail = new WorkRespVO();
        
        if ("book".equals(workType)) {
            // 获取绘本详情
            // 这里需要调用绘本服务获取详情，暂时使用模拟数据
            workDetail.setId(workId);
            workDetail.setTitle("示例绘本");
            workDetail.setAuthor("示例作者");
            workDetail.setCover("https://example.com/cover.jpg");
            workDetail.setDescription("这是一个示例绘本的描述");
            workDetail.setType("book");
            workDetail.setTotalPages(24);
            workDetail.setPermission("allow"); // 实际需要根据用户权限判断
            workDetail.setReadCount(1000);
            workDetail.setLikeCount(500);
        } else if ("bookset".equals(workType)) {
            // 获取绘本集详情
            // 这里需要调用绘本集服务获取详情，暂时使用模拟数据
            workDetail.setId(workId);
            workDetail.setTitle("示例绘本集");
            workDetail.setAuthor("示例作者");
            workDetail.setCover("https://example.com/cover.jpg");
            workDetail.setDescription("这是一个示例绘本集的描述");
            workDetail.setType("bookset");
            workDetail.setTotalPages(120);
            workDetail.setPermission("allow"); // 实际需要根据用户权限判断
            workDetail.setReadCount(2000);
            workDetail.setLikeCount(800);
        }

        // 获取收藏状态
        boolean isCollected = false;
        if (userId != null) {
            CommunityWorkCollectionDO collection = workCollectionMapper.selectByUserIdAndWork(userId, workId, workType);
            isCollected = collection != null;
        }
        workDetail.setIsCollected(isCollected);

        // 获取收藏数
        Long collectCount = workCollectionMapper.countByWork(workId, workType);
        workDetail.setCollectCount(collectCount.intValue());

        return workDetail;
    }

    @Override
    public String checkWorkPermission(Long workId, String workType, Long userId) {
        // 这里需要根据实际的权限逻辑判断
        // 暂时返回允许访问
        return "allow";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LikeToggleRespVO toggleWorkCollection(Long workId, String workType, Long userId) {
        // 1. 参数校验
        CommunityValidationUtils.validateId(workId, "作品ID");
        CommunityValidationUtils.validateWorkType(workType);

        // 2. 查询当前收藏状态
        CommunityWorkCollectionDO existingCollection = workCollectionMapper.selectByUserIdAndWork(userId, workId, workType);
        
        boolean isCollected;
        
        if (existingCollection != null) {
            // 取消收藏
            workCollectionMapper.deleteById(existingCollection.getId());
            isCollected = false;
        } else {
            // 添加收藏
            CommunityWorkCollectionDO collection = CommunityWorkCollectionDO.builder()
                    .userId(userId)
                    .workId(workId)
                    .workType(workType)
                    .build();
            workCollectionMapper.insert(collection);
            isCollected = true;
        }
        
        // 2. 获取最新收藏数
        Long collectCount = workCollectionMapper.countByWork(workId, workType);
        
        // 3. 返回结果
        LikeToggleRespVO result = new LikeToggleRespVO();
        result.setIsLiked(isCollected); // 这里复用点赞的VO，isLiked表示是否收藏
        result.setLikeCount(collectCount.intValue()); // likeCount表示收藏数
        
        log.info("[toggleWorkCollection][作品收藏状态切换] workId: {}, workType: {}, userId: {}, isCollected: {}", 
                workId, workType, userId, isCollected);
        
        return result;
    }

}
